package com.ru.application.creditscore.bean;

import java.io.Serializable;
import java.util.ArrayList;  
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;  
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import com.ru.application.creditscore.domain.Car;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;



public class TableBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private final static String[] colors;  
    private final static String[] manufacturers;  
      
    static {  
        colors = new String[10];  
        colors[0] = "Black";  
        colors[1] = "White";  
        colors[2] = "Green";  
        colors[3] = "Red";  
        colors[4] = "Blue";  
        colors[5] = "Orange";  
        colors[6] = "Silver";  
        colors[7] = "Yellow";  
        colors[8] = "Brown";  
        colors[9] = "Maroon";  
          
        manufacturers = new String[10];  
        manufacturers[0] = "Mercedes";  
        manufacturers[1] = "BMW";  
        manufacturers[2] = "Volvo";  
        manufacturers[3] = "Audi";  
        manufacturers[4] = "Renault";  
        manufacturers[5] = "Opel";  
        manufacturers[6] = "Volkswagen";  
        manufacturers[7] = "Chrysler";  
        manufacturers[8] = "Ferrari";  
        manufacturers[9] = "Ford";  
    }  
  
    private List<Car> cars;  
      
    private Car selectedCar;  
  
    public TableBean() {  
	cars = new ArrayList<Car>();  
        populateRandomCars(cars, 50); 
    }  
      
    public Car getSelectedCar() {  
        return selectedCar;  
    }  
  
    public void setSelectedCar(Car selectedCar) {  
        this.selectedCar = selectedCar;  
    }  
  
    private void populateRandomCars(List<Car> list, int size) {  
        for(int i = 0 ; i < size ; i++)  
            list.add(new Car(getRandomModel(new Integer(i)), getRandomYear(), getRandomManufacturer(), getRandomColor(),false));  
    }  
  
    public List<Car> getCars() {  
        return cars;  
    }  
  
    public void setCars(List<Car> cars) {  
        this.cars = cars;  
    } 
    
    private int getRandomYear() {  
        return (int) (Math.random() * 50 + 1960);  
    }  
      
    private String getRandomColor() {  
        return colors[(int) (Math.random() * 10)];  
    }  
      
    private String getRandomManufacturer() {  
        return manufacturers[(int) (Math.random() * 10)];  
    }  
      
    private Integer getRandomModel(Integer model) {  
        return model;  
    }  
    
    public void removeCar(ActionEvent actionEvent){
	String commandId = actionEvent.getComponent().getId();
	System.out.println("commandId=" + commandId);
	if(commandId.compareToIgnoreCase("confirm") == 0) {
	    if(selectedCar != null) {
		System.out.println("getSelectedCar()=" + selectedCar.getName());
		cars.remove(selectedCar);
		System.out.println("new size()=" + cars.size());
	    } else {
		System.out.println("selectedCar is null");
	    }
	} 
	
    }
    
    public void changeCar() {  
	System.out.println("changeCar");
	Car car = (Car)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("car"); 
	System.out.println(car.getName());
	System.out.println(car.getYear());
	this.setSelectedCar(car);
	
    } 
    
    public void deleteSelectedCar() {  
   	System.out.println("deleteSelectedCar");
   	Car car = (Car)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("car"); 
    } 
}
